home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus Extra 1996 #3 / AmigaPlus_CD-ROM-EXTRA_Nr.3.bin / aminet-spiele / role on / labyrinthii / ss.c < prev    next >
C/C++ Source or Header  |  1988-10-02  |  995b  |  44 lines

  1.  
  2. /* Scanshort takes string as argument and returns short integer from beginning
  3.  * of string - will not scan through string looking for numbers. If invalid,
  4.  * returns 32767 and puts '\0' in first char of string. (C) 1987 Russell
  5.  * Wallace - this routine may be freely distributed and used for program
  6.  * development. Use +L compile option with Aztec C. */
  7.  
  8. short scanshort (string)
  9. char *string;
  10. {
  11.     register short i;
  12.     register long number=0L;
  13.     register short digit=1;
  14.     register short minus=0;
  15.     while ((string[0]<'0' || string[0]>'9')&& string[0] && string[0]!='-')
  16.         string++;
  17.     if (string[0]=='-')
  18.     {
  19.         minus++;
  20.         string[0]='0';
  21.     }
  22.     for (i=0;string[i]>='0' && string[i]<='9';i++)
  23.         ;
  24.     if (i==0 || i>6)
  25.     {
  26.         string[0]='\0';
  27.         return (32767);
  28.     }
  29.     do
  30.     {
  31.         number+=(string[--i]-'0')*digit;
  32.         digit*=10;
  33.     }
  34.     while (i);
  35.     if ((number-minus) & (long)(0xFFFF8000))
  36.     {
  37.         string[0]='\0';
  38.         return (32767);
  39.     }
  40.     if (minus)
  41.         number=(~number)+1;        /* 2's complement sign change */
  42.     return ((short)number);
  43. }
  44.